//
//  MineViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/13.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "MineViewController.h"
#import "UserInfo.h"
#import "SettingViewController.h"
#import "TokenViewController.h"

@interface MineViewController (){
    UserInfo *userInfo;
}
@property (nonatomic,strong) NSArray *listArray;

@property (nonatomic,strong) UILabel *asertValueLabel;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    userInfo = [Utils getUserInfo];
    self.tableView.frame = CGRectMake(0, StatusBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight);
    self.tableView.tableHeaderView = [self getHeaderView];
    self.tableView.separatorStyle = 0;
    self.listArray = @[@"我的通证",@"我的订单",@"设置"];
    
}

-(UIView*)getHeaderView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 230)];
//    view.backgroundColor = [UIColor whiteColor];
    UILabel *nameLabel = [UILabel new];
    nameLabel.text = userInfo.nickName;
    nameLabel.font = [UIFont systemFontOfSize:25];
    [view addSubview:nameLabel];
    nameLabel.sd_layout.leftSpaceToView(view, 20).topSpaceToView(view, 20).heightIs(28).autoWidthRatio(0);
    [nameLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UILabel *lastTimeLabel = [UILabel new];
    lastTimeLabel.text = @"上次登录时间:2018-02-11 12:12:12";
    lastTimeLabel.font = [UIFont systemFontOfSize:12];
    lastTimeLabel.textColor = [UIColor grayColor];
    [view addSubview:lastTimeLabel];
    lastTimeLabel.hidden = YES;
    lastTimeLabel.sd_layout.leftSpaceToView(view, 20).topSpaceToView(nameLabel, 5).heightIs(20).autoWidthRatio(0);
    [lastTimeLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIImageView *phoneBackImageView = [UIImageView new];
    [phoneBackImageView setImage:[UIImage imageNamed:@"phone_back"]];
    [view addSubview:phoneBackImageView];
    phoneBackImageView.sd_layout.leftSpaceToView(view, 20).topSpaceToView(lastTimeLabel, 5).heightIs(20).widthIs(100);

    
    UILabel *phoneLabel = [UILabel new];
    phoneLabel.text = userInfo.phone;
    phoneLabel.textAlignment = NSTextAlignmentCenter;
    phoneLabel.font = [UIFont systemFontOfSize:12];
    phoneLabel.textColor = [UIColor whiteColor];
    [view addSubview:phoneLabel];
    phoneLabel.sd_layout.leftSpaceToView(view, 20).topSpaceToView(lastTimeLabel, 5).heightIs(20).widthIs(100);
//    [phoneLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIImageView *headImageView = [UIImageView new];
//    headImageView.backgroundColor = [UIColor brownColor];
    headImageView.layer.cornerRadius = 85/2;
    headImageView.layer.masksToBounds = YES;
    headImageView.layer.borderWidth = 1.0;
    headImageView.layer.borderColor = [UIColor blackColor].CGColor;
    headImageView.contentMode = UIViewContentModeCenter;
    headImageView.image = [UIImage imageNamed:@"headicon"];
    [view addSubview:headImageView];
    headImageView.sd_layout.rightSpaceToView(view, 20).topSpaceToView(view, 15).widthIs(85).heightIs(85);
    
    UILabel *asertLabel = [UILabel new];
    asertLabel.text = @"我的财富";
    asertLabel.font = [UIFont systemFontOfSize:20];
    asertLabel.textColor = [UIColor blackColor];
    [view addSubview:asertLabel];
    asertLabel.sd_layout.leftSpaceToView(view, 20).topSpaceToView(phoneLabel, 20).heightIs(20).autoWidthRatio(0);
    [asertLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIButton *hiddenButton = [UIButton new];
    hiddenButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [hiddenButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    if([[[FileOperator defaultOperator] userInfoObjectForKey:@"showM"] boolValue]){
        [hiddenButton setTitle:@"显示" forState:UIControlStateNormal];
        hiddenButton.selected = YES;
    }else{
        [hiddenButton setTitle:@"隐藏" forState:UIControlStateNormal];
        hiddenButton.selected = NO;

    }
    [hiddenButton addTarget:self action:@selector(hiddenAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:hiddenButton];
    hiddenButton.sd_layout.leftSpaceToView(asertLabel, 10).topSpaceToView(phoneLabel, 23).heightIs(20).widthIs(30);
    
    UIButton *asertDetailButton = [UIButton new];
    asertDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    asertDetailButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [asertDetailButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [asertDetailButton setTitle:@"财富详情" forState:UIControlStateNormal];
    [view addSubview:asertDetailButton];
    asertDetailButton.sd_layout.rightSpaceToView(view, 20).topSpaceToView(phoneLabel, 23).heightIs(20).widthIs(100);
    asertDetailButton.hidden = YES;
    
    UILabel *asertNameLabel = [UILabel new];
    asertNameLabel.text = @"资产";
    asertNameLabel.font = [UIFont systemFontOfSize:14];
    asertNameLabel.textColor = [UIColor blackColor];
    [view addSubview:asertNameLabel];
    asertNameLabel.sd_layout.leftSpaceToView(view, 20).topSpaceToView(asertLabel, 30).heightIs(20).autoWidthRatio(0);
    [asertNameLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UILabel *asertValueLabel = [UILabel new];
    if([[[FileOperator defaultOperator] userInfoObjectForKey:@"showM"] boolValue]){
        asertValueLabel.text =  @"*****元";

    }else{
        asertValueLabel.text = [NSString stringWithFormat:@"%.2f元",userInfo.orgAccount.balance];

    }
    asertValueLabel.font = [UIFont systemFontOfSize:20];
    asertValueLabel.textColor = [UIColor blackColor];
    [view addSubview:asertValueLabel];
    asertValueLabel.sd_layout.leftSpaceToView(view, 20).topSpaceToView(asertNameLabel, 5).heightIs(20).autoWidthRatio(0);
    [asertValueLabel setSingleLineAutoResizeWithMaxWidth:200];
    self.asertValueLabel = asertValueLabel;
    
    UILabel *pointNameLabel = [UILabel new];
    pointNameLabel.text = @"积分";
    pointNameLabel.font = [UIFont systemFontOfSize:14];
    pointNameLabel.textColor = [UIColor blackColor];
    [view addSubview:pointNameLabel];
    pointNameLabel.sd_layout.leftSpaceToView(view, 20+SCREEN_WIDTH/2).topSpaceToView(asertLabel, 30).heightIs(20).autoWidthRatio(0);
    [pointNameLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UILabel *pointValueLabel = [UILabel new];
    pointValueLabel.text = [NSString stringWithFormat:@"%d分",userInfo.point];
    pointValueLabel.font = [UIFont systemFontOfSize:20];
    pointValueLabel.textColor = [UIColor blackColor];
    [view addSubview:pointValueLabel];
    pointValueLabel.sd_layout.leftSpaceToView(view, 20+SCREEN_WIDTH/2).topSpaceToView(asertNameLabel, 5).heightIs(20).autoWidthRatio(0);
    [pointValueLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    return view;
}

-(void)hiddenAction:(id)sender{
    UIButton *button = sender;
    button.selected = !button.selected;
    if (button.selected) {
        [button setTitle:@"显示" forState:UIControlStateNormal];
        self.asertValueLabel.text = @"*****元";
    }else{
        [button setTitle:@"隐藏" forState:UIControlStateNormal];
        self.asertValueLabel.text = [NSString stringWithFormat:@"%.2f元",userInfo.orgAccount.balance];
    }
    
    [[FileOperator defaultOperator] setUserInfoObject:[NSNumber numberWithBool:button.selected] forKey:@"showM"];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = 0;
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UIImageView *arrowImageView = [UIImageView new];
        arrowImageView.contentMode = UIViewContentModeCenter;
        arrowImageView.image = [UIImage imageNamed:@"arrow_right"];
        [cell.contentView addSubview:arrowImageView];
        arrowImageView.sd_layout.topSpaceToView(cell.contentView, 0).bottomSpaceToView(cell.contentView, 0).rightSpaceToView(cell.contentView, 10).widthIs(10);
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = DefaultCellLineColor;
        [cell.contentView addSubview:lineView];
        lineView.sd_layout.leftSpaceToView(cell.contentView, 10).bottomSpaceToView(cell.contentView, 0).rightSpaceToView(cell.contentView, 0).heightIs(1);
    }
    // Configure the cell...
    cell.textLabel.text = self.listArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        SettingViewController *settingViewController = [[SettingViewController alloc] init];
        [self.navigationController pushViewController:settingViewController animated:YES];
    }else if (indexPath.row == 0){
        TokenViewController *tokenViewController = [[TokenViewController alloc] init];
        [self.navigationController pushViewController:tokenViewController animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
